<?php
/**
 * mailer.class
 * 
 * Caching mechanizm
 * 
 * @version:	0.2b
 * @package:	core
 * @author:	alex_ez
 * @created:	21 dec 2008
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */

class Mailer_Exception extends Exception { }

class mailer extends singleton
{
	// php singleton wrapper
	static public function &instance() { return parent::instance(__CLASS__); }
	
	protected $path = './views/.mail/';
	protected $view = null;
	protected $config = null;
	
	// changeable
	protected $from = null;
	protected $host = null;
	protected $subject = null;
	protected $debug = false;

	protected function __construct( ) {

		$this->config	= config::instance();
		$this->view = view::instance();
		
		$this->path	= pick( @$this->config->mailer__templates, '' );
		
		$this->host	= pick( $this->config->mailer__host, $this->config->host, @$_SERVER['SERVER_NAME'] );
		$this->from	= pick( $this->config->mailer__email, @$_SERVER['SERVER_ADMIN'], 'no-reply' );
		$this->subject = pick( $this->config->mailer__subject, $this->config->meta__title );
	}
	
	public function __set( $k, $v ) {

		$k = @strtolower($k);
		if(!$k || !strpos(',from,host', $k)) return false;
		
		switch($k) {
		case 'host':
			if( !texo::is_Domain($v) ) throw new Mailer_Exception( "host ".$v." not valid" );
			$this->host = $v;
			$this->from = 'no-reply';
		break;
		case 'from':
			if (!texo::is_email($v)) {
				if (!is_string($v)) throw new Mailer_Exception( "from ".$v." not valid" );
				$v = texo::latinate($v);
				$this->from = sprintf('%s@%s', $v, $this->host);
				break;
			}
			$this->from = $v;
		break;
		case 'debug':
			$this->debug = $v;
			break;
		default: return null;
		}
		
		return true;
	}
	
	public function __get( $k )
	{
		$k = @strtolower( $k );
		if( !$k || strpos( ',from,host', $k ) === false ) return false;
		
		switch( $k )
		{
		case 'host':	return $this->host;
		case 'from':	list( $r, ) = explode( '@', $this->from ); return $r;
		case 'debug':	return $this->debug;
		}
		return null;
	}
	
	// deprecated
	public function send( $o, $subject = null, $message = null )
	{
		// old style
		if( texo::is_email( $o ) )
		{
			$o = array( 'to' => $o );
			if( !is_null( $subject ) ) $o['subject'] = $subject;
			if( !is_null( $message ) ) $o['body'] = $message;
		}
		
		// checkups
		if( !is_array($o) )		throw new Mailer_Exception( "where is parameters?" );
		if( !isset($o['to']) )		throw new Mailer_Exception( "sorry, who waits for a message?" );
		if( !texo::is_email($o['to']) )	throw new Mailer_Exception( "to '{$o['to']}' not a valid email" );
		if( !texo::is_email($this->from) ) throw new Mailer_Exception( "from '{$this->from}' not a valid email? oh... fuck!" );
		
		// default fields
		$_defaults = array(
			'template' => '_default.tpl',
			'subject' => $this->subject,
			);
		
		// fetch fields
		foreach( array( 'template', 'subject' ) as $k )
			if( !isset( $o[$k] ) ) $o[$k] = $_defaults[$k];
		foreach( array( 'from', 'host' ) as $k )
			if( !isset( $o[$k] ) ) $o[$k] = $this->$k;
		
		$o['template'] = preg_replace( '/\.tpl$/i', '', $o['template'] ).'.tpl';
		if ($this->debug && bc::enabled()) bc::log( $o, 'mailer.send( options )' );
		
		// create body or fetch ready
		if( !isset( $o['body'] ) || !is_string( $o['body'] ) ) {
			$templates = array_unique( array( $this->from.'@'.$o['template'], $o['template'], $this->from.'@'.'_default.tpl', '_default.tpl' ) );
			$templates = array_map( create_function( '$v', 'return "'.$this->path.'".$v;' ), $templates );
			if ($this->debug && bc::enabled()) bc::log( $templates );
			unset( $o['template'] );
			$o['body'] = $this->view->display( $templates, $o );
			if ($this->debug && bc::enabled()) bc::log( $o['body'], 'body' );
		}
		
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
		//$headers .= 'To: ' . $o['to'] . "\r\n";
		$headers .= 'From: ' . $this->from . "\r\n";
		
		//foreach( array( 'template', 'body', 'o' ) as $k ) if (bc::enabled()) bc::log( $$k, $k );
		if ($this->debug && bc::enabled()) bc::log( $headers.'To: ' . $o['to']."\r\nSubject: ".$o['subject']."\r\n\r\n".$o['body'], 'content' );
		mail( $o['to'], $o['subject'], $o['body'], $headers );
	}
}


